#!/usr/bin/perl
use strict;
use warnings;
use File::Copy;
use File::Basename;

#####################################
##### CHECK/ CHANGE LINES BELOW #####
#####################################
my $sid = "JET"; # SID of DB i.e.: MAX
my $nr_wait = 2; # wait x * 5min , max. 1 hours

my $maxdb_usr_connect = '-u control,dbs12jos'; # connection string user,password - i.e. '-u control,control'
my $maxdb_util_connect = '-uUTL control,dbs12jos'; # connection string utility session user,password - i.e. '-uUTL control,control'

# dbmcli binary - use "/" for paths also in Windows!
# i.e. "/sapdb/programs/bin/dbmcli" or "E:/sapdb/programs/pgm/dbmcli.exe"
my $dbmcli_bin = '/sapdb/programs/bin/dbmcli';

#####################################
##### DO NOT CHANGE LINES BELOW #####
#####################################
umask 0111; # setting umask thus file permission is 666
my $db_connection = "$maxdb_usr_connect $maxdb_util_connect"; # putting together whole connection string
#my $backup_command = "$dbmcli_bin -d $sid $db_connection backup_start NSR_DB_$sid DATA"; #putting together whole backup command
my $check_state_command = "$dbmcli_bin -d $sid $db_connection db_state"; # get db_state

my $state_result = `$check_state_command`; # running backup command - external usage of dbmcli - getting return message
chomp $state_result; # deleting newline in return message
$state_result =~ s/\s+/ /g; # replace mulitple spaces with one space



print "getting database state:\n";
print "$state_result\n";
print "changing database state into admin mode:\n";
my $change_db_mode = "$dbmcli_bin -d $sid $db_connection db_admin"; # change db_state
my $change_db_mode_result = `$change_db_mode`; # running backup command - external usage of dbmcli - getting return message
chomp $change_db_mode_result; # deleting newline in return message
$change_db_mode_result =~ s/\s+/ /g; # replace mulitple spaces with one space
print "$change_db_mode_result\n";
$state_result = `$check_state_command`; # running backup command - external usage of dbmcli - getting return message
chomp $state_result; # deleting newline in return message
$state_result =~ s/\s+/ /g; # replace mulitple spaces with one space
print "$state_result\n";
print "clear log of database\n";
my $clear_log_command = "$dbmcli_bin -d $sid $db_connection util_execute clear log"; # clear log
my $clear_log_command_result = `$clear_log_command`; # running backup command - external usage of dbmcli - getting return message
chomp $clear_log_command_result; # deleting newline in return message
$clear_log_command_result =~ s/\s+/ /g; # replace mulitple spaces with one space
print "$clear_log_command_result\n";

print "and now: restoring database from first offline backup of 03.10.2014 - AVAILABLE|NST 1412346690 josvjet0.net120.os.itelligence.de|DATA  RECOVERY |2014-10-03 16:31:30|
\n";
my $restore_command = "$dbmcli_bin -d $sid $db_connection recover_start NSR_DB_JET DB EBID \"NST 1412346690 josvjet0.net120.os.itelligence.de\""; # restore database
my $restore_command_result = `$restore_command`; # running backup command - external usage of dbmcli - getting return message
chomp $restore_command_result; # deleting newline in return message
$restore_command_result =~ s/\s+/ /g; # replace mulitple spaces with one space
print "$restore_command_result\n";

exit 0;

